package com.base.chat.ac;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.chat.R;
import com.base.chat.adapter.TabFragmentAdapter;
import com.base.chat.databinding.ActivityAddFriendsBinding;
import com.base.chat.fragment.SearchFriendsListFragment;
import com.base.chat.fragment.SearchGroupListFragment;
import com.base.chat.util.ChatStatEntity;
import com.base.commonlib.http.utils.ScaleUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ScreenUtil;
import com.base.video.CBaseVideoBarActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


@Route(path = "/friend/add")
public class AddFriendsActivity extends CBaseVideoBarActivity {

    private ActivityAddFriendsBinding mBinding;
    private SearchFriendsListFragment searchFriendsListFragment;
    private SearchGroupListFragment searchGroupListFragment;
    private int type = 0; //0 搜索好友  1 搜索群组
    private int mIndex = 0;
    private String mTitles[] = {
            "好友", "群聊"};
    private List<Fragment> mFragments;
    private TabFragmentAdapter mTabFragmentAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_add_friends;
    }


    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityAddFriendsBinding) mRootBinding;
        mFragments = new ArrayList<>();
        searchFriendsListFragment = new SearchFriendsListFragment();
        searchGroupListFragment = new SearchGroupListFragment();
        mFragments.add(searchFriendsListFragment);
        mFragments.add(searchGroupListFragment);

        mTabFragmentAdapter = new TabFragmentAdapter(mFragments, mTitles, getSupportFragmentManager(), mContext);
//        mViewPager.setOffscreenPageLimit(mFragments.size());// 设置预加载Fragment个数
        mBinding.viewPager.setOffscreenPageLimit(0);// 设置预加载Fragment个数
        mBinding.viewPager.setAdapter(mTabFragmentAdapter);
        mBinding.viewPager.setCurrentItem(0);// 设置当前显示标签页为第一页

        // 将ViewPager和TabLayout绑定
        mBinding.tablayout.setupWithViewPager(mBinding.viewPager);
        // 设置tab文本的没有选中（第一个参数）和选中（第二个参数）的颜色
        mBinding.tablayout.setTabTextColors(Color.parseColor("#888888"), Color.parseColor("#3072F3"));
        reflex(mBinding.tablayout);
    }


    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("添加好友");
        mBinding.search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //mPresenter.getList(s.toString());
                ChatStatEntity chatStatEntity = new ChatStatEntity();
                chatStatEntity.setState(0X0000010);
                chatStatEntity.setData(s.toString());
                RxBus2.getInstance().post(chatStatEntity);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
                    int dp10 = ScaleUtil.dp2px(tabLayout.getContext(), 0);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        tabView.setPadding(0, 0, 0, 0);
                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
